package com.bhprod.guitartrack;

import org.fmod.FMODAudioDevice;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

/*
 * TODO :
 * Voir https://code.google.com/p/range-seek-bar/
 */

public class MainActivity extends Activity {

	private FMODAudioDevice mFMODAudioDevice = new FMODAudioDevice();
	private String filePath;
	private int paused = 1;
	private int speed = 10;

	private Handler mUpdateHandler = new Handler() {
		public void handleMessage(Message msg) {
			cUpdate();
			
			// Update seekbar position
			int position = cGetPosition();
			SeekBar seekBar = (SeekBar)findViewById(R.id.trackSeekBar);
			seekBar.setProgress(position);

			removeMessages(0);
			sendMessageDelayed(obtainMessage(0), 50);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
	    filePath = intent.getStringExtra("file");
		
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		mFMODAudioDevice.start();
		cBegin(filePath);
		mUpdateHandler.sendMessageDelayed(mUpdateHandler.obtainMessage(0), 0);
		cPlaySound();
		
		// Set seekbar size
		int trackLength = cGetLength();
		SeekBar seekBar = (SeekBar)findViewById(R.id.trackSeekBar);
		seekBar.setMax(trackLength);
		
		seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int progress = seekBar.getProgress();
				cSetPosition(progress);

				paused = 0;
				cPauseSound(paused);
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				paused = 1;
				cPauseSound(paused);
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}
		});
		
		setTrackName();
		
		setSpeedText();
	}

	private void setSpeedText() {
		TextView speedDisplay = (TextView)findViewById(R.id.speedText);
		speedDisplay.setText(String.valueOf(speed/10f));
	}

	private void setTrackName() {
		TextView trackName = (TextView)findViewById(R.id.trackName);
		trackName.setText(cGetTitle());
	}

	@Override
	protected void onStop() {
		mUpdateHandler.removeMessages(0);
    	cEnd();
		mFMODAudioDevice.stop();
		super.onStop();
	}
	
	public void playPause(View button) {
		if (paused == 0) {
			paused = 1;
			((ImageButton)button).setImageDrawable(getResources().getDrawable(R.drawable.play));
		} else {
			paused = 0;
			((ImageButton)button).setImageDrawable(getResources().getDrawable(R.drawable.pause));
		}
		cPauseSound(paused);
	}
	
	public void decreaseSpeed(View button) {
		speed += 1;
		if (speed > 20) {
			speed = 20;
		}
		setSpeedText();
		cSetSpeed(speed / 10f);
	}
	
	public void increaseSpeed(View button) {
		speed -= 1;
		if (speed < 5) {
			speed = 5;
		}
		setSpeedText();
		cSetSpeed(speed / 10f);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

	static {
		System.loadLibrary("fmodex");
		System.loadLibrary("main");
	}
	
	public native void cBegin(String filePath);
	public native void cUpdate();
	public native void cEnd();
	public native void cPlaySound();
	public native void cPauseSound(int paused);
	public native String cGetTitle();
	public native void cSetSpeed(float speed);
	/**
	 * Return track length in milliseconds
	 * @return the length in milliseconds
	 */
	public native int cGetLength();
	/**
	 * Return the current track position in milliseconds
	 * @return the position in milliseconds
	 */
	public native int cGetPosition();
	/**
	 * Set the current track position in milliseconds
	 * @param position the position in milliseconds
	 */
	public native void cSetPosition(int position);

}
